/*
 * @Description:
 * @Version: 1.0
 * @Autor: 张晨
 * @Date: 2021-12-15 14:36:48
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-01-14 20:12:38
 */
import axios from 'axios'
import { JSEncrypt } from 'jsencrypt'

export default {
  passwordEncryption(password) {
    // 从服务器上获取apaasRsa.pub
    return new Promise(async (resolve, reject) => {
      let publicKeyUrl
      if(location.hostname !== 'localhost'){
        publicKeyUrl = window.location.protocol + '//' + window.location.host + '/workbench/' + 'apaasRsa.pub'
      }else{
        publicKeyUrl = 'http://172.19.11.93/workbench/apaasRsa.pub'
      }
      try {
        const publicKey = (await axios.get(publicKeyUrl)).data
        let encryptor = new JSEncrypt() // 新建JSEncrypt对象
        encryptor.setPublicKey(publicKey) // 设置公钥
        let passwordEncryp = encryptor.encrypt(password) // 对密码进行加密
        resolve(passwordEncryp)
      } catch (err) {
        reject(err)
      }
    })
  }
}
